package moe.net.service.impl;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import moe.net.dto.*;
import moe.net.enums.ReportStateEnum;
import moe.net.mapper.ReportDetailApiMapper;
import moe.net.mapper.ReportDetailStressMapper;
import moe.net.mapper.ReportDetailUiMapper;
import moe.net.mapper.ReportMapper;
import moe.net.model.ReportDO;
import moe.net.model.ReportDetailApiDO;
import moe.net.model.ReportDetailStressDO;
import moe.net.model.ReportDetailUiDO;
import moe.net.service.ReportDetailService;
import moe.net.util.JsonUtil;
import moe.net.util.SpringBeanUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ReportDetailServiceImpl implements ReportDetailService {

    @Resource
    private ReportDetailStressMapper reportDetailStressMapper;

    @Resource
    private ReportMapper reportMapper;

    @Resource
    private ReportDetailApiMapper reportDetailApiMapper;

    @Resource
    private ReportDetailUiMapper reportDetailUiMapper;

    @Override
    public void handleStressReportDetail(String topicContent) {

        StressSampleResultDTO stressSampleResultDTO = JsonUtil.json2Obj(topicContent, StressSampleResultDTO.class);

        ReportDetailStressDO reportDetailStressDO = SpringBeanUtil.copyProperties(stressSampleResultDTO, ReportDetailStressDO.class);

        reportDetailStressMapper.insert(reportDetailStressDO);

    }

    @Override
    public void handleApiReportDetail(String reportContent) {

        ApiCaseResultDTO apiCaseResultDTO = JsonUtil.json2Obj(reportContent, ApiCaseResultDTO.class);

        //处理测试报告的概述
        ReportDO reportDO = reportMapper.selectById(apiCaseResultDTO.getReportId());
        reportDO.setExecuteState(ReportStateEnum.EXECUTE_SUCCESS.name());
        reportDO.setEndTime(apiCaseResultDTO.getEndTime());
        reportDO.setExpandTime(apiCaseResultDTO.getExpendTime());
        reportDO.setQuantity(Long.valueOf(apiCaseResultDTO.getQuantity()));
        reportDO.setFailQuantity(Long.valueOf(apiCaseResultDTO.getFailQuantity()));
        reportDO.setPassQuantity(reportDO.getQuantity() - reportDO.getFailQuantity());
        //更新概述
        reportMapper.updateById(reportDO);

        //处理测试报告明细
        List<ApiCaseResultItemDTO> stepList = apiCaseResultDTO.getList();
        stepList.forEach(item -> {

            ReportDetailApiDO reportDetailApiDO = SpringBeanUtil.copyProperties(item, ReportDetailApiDO.class);
            ApiCaseStepDTO step = item.getApiCaseStepDTO();
            reportDetailApiDO.setEnvironmentId(step.getEnvironmentId());
            reportDetailApiDO.setCaseId(step.getCaseId());
            reportDetailApiDO.setNum(step.getNum());
            reportDetailApiDO.setName(step.getName());
            reportDetailApiDO.setDescription(step.getDescription());
            reportDetailApiDO.setAssertion(step.getAssertion());
            reportDetailApiDO.setRelation(step.getRelation());
            reportDetailApiDO.setPath(step.getPath());
            reportDetailApiDO.setMethod(step.getMethod());
            reportDetailApiDO.setQuery(step.getQuery());
            reportDetailApiDO.setHeader(step.getHeader());
            reportDetailApiDO.setBody(step.getBody());
            reportDetailApiDO.setBodyType(step.getBodyType());
            reportDetailApiMapper.insert(reportDetailApiDO);
        });


    }


    @Override
    public void handleUiReportDetail(String reportContent) {
        UiCaseResultDTO uiCaseResultDTO = JsonUtil.json2Obj(reportContent, UiCaseResultDTO.class);
        ReportDO reportDO = reportMapper.selectById(uiCaseResultDTO.getReportId());
        reportDO.setExecuteState(ReportStateEnum.EXECUTE_SUCCESS.name());
        reportDO.setEndTime(uiCaseResultDTO.getEndTime());
        reportDO.setExpandTime(uiCaseResultDTO.getExpendTime());
        reportDO.setQuantity(Long.valueOf(uiCaseResultDTO.getQuantity()));
        reportDO.setFailQuantity(Long.valueOf(uiCaseResultDTO.getFailQuantity()));
        reportDO.setPassQuantity(reportDO.getQuantity() - reportDO.getFailQuantity());

        reportMapper.updateById(reportDO);

        //处理报告明细
        List<UiCaseResultItemDTO> stepList = uiCaseResultDTO.getList();
        stepList.forEach(item->{
            ReportDetailUiDO reportDetailUiDO = SpringBeanUtil.copyProperties(item, ReportDetailUiDO.class);
            UiCaseStepDTO uiCaseStep = item.getUiCaseStep();
            SpringBeanUtil.copyProperties(uiCaseStep, reportDetailUiDO);
            reportDetailUiDO.setId(null);
            reportDetailUiMapper.insert(reportDetailUiDO);
        });


    }


}
